﻿Imports System.Data.OleDb
Imports System.Web.UI.DataVisualization

Public Class details
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load


        If User.Identity.Name = "" Then
            Panel1.Visible = False
        End If

        Dim dr As OleDbDataReader
        Try
            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
            oleDbConn.Open()
            Dim TagExistCmd As OleDbCommand = New OleDbCommand("SELECT Hausaterm FROM Terms Where TermID=@F1", oleDbConn)
            TagExistCmd.Parameters.AddWithValue("@f1", Request.QueryString("TermID"))
            dr = TagExistCmd.ExecuteReader()
            dr.Read()
            TB_Title.Text = "Re : " & dr.GetValue(0)
            oleDbConn.Close()
        Catch ex As Exception
            TB_Title.Text = ""
        End Try

        Dim dr2 As OleDbDataReader
        Try
            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
            oleDbConn.Open()
            Dim TagExistCmd As OleDbCommand = New OleDbCommand("SELECT 1 FROM TermLikes Where TermID=@f1 and UserID = @f2", oleDbConn)
            TagExistCmd.Parameters.AddWithValue("@f1", Request.QueryString("TermID"))
            TagExistCmd.Parameters.AddWithValue("@f1", User.Identity.Name)
            dr2 = TagExistCmd.ExecuteReader()
            dr2.Read()
            If dr2.GetValue(0) = 1 Then
                IM_Like.Visible = True
                Btn_Like.Visible = False
            End If
            oleDbConn.Close()
        Catch ex As Exception
            IM_Like.Visible = False
            Btn_Like.Visible = True
        End Try


        Dim dr3 As OleDbDataReader
        Try
            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
            oleDbConn.Open()
            Dim TagExistCmd As OleDbCommand = New OleDbCommand("SELECT rate FROM RatingTable Where TermID=@f1 and UserID = @f2", oleDbConn)
            TagExistCmd.Parameters.AddWithValue("@f1", Request.QueryString("TermID"))
            TagExistCmd.Parameters.AddWithValue("@f1", User.Identity.Name)
            dr3 = TagExistCmd.ExecuteReader()
            dr3.Read()
            If Not IsDBNull(dr3("rate")) Then lblRating.Text = dr3("rate")
            oleDbConn.Close()
        Catch ex As Exception

        End Try

    End Sub

    Protected Sub btn_Rate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_Rate.Click

        Dim oleDbConn As New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
        oleDbConn.Open()
        Dim Sql As String = "DELETE FROM RatingTable WHERE TermID=@f1 AND UserID=@f2"
        Dim cmd As New OleDbCommand(Sql, oleDbConn)
        cmd.Parameters.AddWithValue("@f1", Request.QueryString("TermID"))
        cmd.Parameters.AddWithValue("@f2", User.Identity.Name)
        cmd.ExecuteNonQuery()


        Dim Sql2 As String = "INSERT INTO RatingTable (TermID,UserID,Rate) VALUES (@f1,@f2,@f3)"
        Dim cmd2 As New OleDbCommand(Sql2, oleDbConn)
        cmd2.Parameters.AddWithValue("@f1", Request.QueryString("TermID"))
        cmd2.Parameters.AddWithValue("@f2", User.Identity.Name)
        cmd2.Parameters.AddWithValue("@f3", Rating1.CurrentRating)
        cmd2.ExecuteNonQuery()
        oleDbConn.Close()

        Response.Redirect("Details.aspx?TermID=" & Request.QueryString("TermID"))

    End Sub

    Protected Sub AddTag_Click(ByVal sender As Object, ByVal e As EventArgs) Handles AddTag.Click



        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
        Dim Sql As String = "INSERT INTO TermsTags (TermID, TagID) VALUES (@f1,@f2)"
        oleDbConn.Open()

        For Each cb As ListItem In CheckBoxList1.Items
            If cb.Selected Then
                Dim cmd As OleDbCommand = New OleDbCommand(Sql, oleDbConn)
                cmd.CommandType = CommandType.Text
                cmd.Parameters.AddWithValue("@f1", Request.QueryString("TermID"))
                cmd.Parameters.AddWithValue("@f2", cb.Value)
                cmd.ExecuteNonQuery()
            End If
        Next

        If TextBox1.Text = "" Then Return

        Dim newTags = TextBox1.Text.Split(",")

        For Each newTag In newTags

            Dim TagId As Double
            Dim dr As OleDbDataReader

            Try
                Dim TagExistCmd As OleDbCommand = New OleDbCommand("SELECT TagID FROM Tags Where Tag=@F1", oleDbConn)
                TagExistCmd.Parameters.AddWithValue("@f1", newTag)
                dr = TagExistCmd.ExecuteReader()
                dr.Read()
                TagId = dr.GetValue(0)
            Catch ex As Exception
                TagId = 0
            End Try

            If TagId <= 0 Then

                Dim newTagsql As String = "INSERT INTO Tags(Tag) VALUES(@f1)"
                Dim cmd2 As OleDbCommand = New OleDbCommand(newTagsql, oleDbConn)
                cmd2.CommandType = CommandType.Text
                cmd2.Parameters.AddWithValue("@f1", newTag)
                cmd2.ExecuteNonQuery()

            End If
        Next
        Response.Redirect("Details.aspx?TermID=" & Request.QueryString("TermID"))
    End Sub

    Protected Sub btn_submit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_submit.Click

        Dim sql As String = "Insert into Commentstable (CTitle, Ctext, UserID, TermID, AddedDate) VALUES (@f1, @f2, @f3, @f4, NOW())"
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
        Dim cmd3 As OleDbCommand = New OleDbCommand(sql, conn)
        cmd3.Parameters.AddWithValue("@f1", TB_Title.Text)
        cmd3.Parameters.AddWithValue("@f2", TB_Comment.Text)
        cmd3.Parameters.AddWithValue("@f3", User.Identity.Name)
        cmd3.Parameters.AddWithValue("@f4", Request.QueryString("TermID"))
        conn.Open()
        cmd3.ExecuteNonQuery()
        conn.Close()
        cmd3.Dispose()
        conn.Dispose()

        Response.Redirect("Details.aspx?TermID=" & Request.QueryString("TermID"))
    End Sub

    Protected Sub Btn_Like_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Btn_Like.Click


        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
        Dim Sql As String = "INSERT INTO TermLikes (TermID, UserID) VALUES (@f1,@f2)"
        oleDbConn.Open()

        Dim cmd2 As New OleDbCommand(Sql, oleDbConn)
        cmd2.Parameters.AddWithValue("@f1", Request.QueryString("TermID"))
        cmd2.Parameters.AddWithValue("@f2", User.Identity.Name)
        cmd2.ExecuteNonQuery()

        IM_Like.Visible = True
        Btn_Like.Visible = False
    End Sub

  
End Class